@extends(Request::ajax() ? 'layouts.partial' : 'layouts.app')
@section('content')

@if (isset ($object))
  {{ Form::open(array('url' => '/' . $table . '/' . $object->id, 'method'=>'put')) }}
@else
  {{ Form::open(array('url' => '/' . $table, 'method'=>'post')) }}
@endif

<?php
$first = true;
?>

@foreach ($fields as $field => $arr)

  <div class="form-group">
    {{ Form::label($field, $arr['label']) }}

<?php
// could use model binding to do all this
if (isset ($arr['value'])) {
    $val = $arr['value'];
} else if (isset ($object)) {
    $val = $object->$field;
} else {
    $val = Input::old($field);
}
switch ($arr['type']) {
    case 'select':        
        echo Form::select($field, $arr['options'], $val);
        break;
    default:
        $options = array('class' => 'form-control');
        if ($first) {
            $options['autofocus'] = 'autofocus';
        }
        echo Form::text($field, $val, $options);
        break;
} 
$first = false;
?>
    </div>

@endforeach
<br />
  {{ Form::submit(isset ($object) ? 'update' : 'create', array('class' => 'button')) }}
&nbsp; <a href='/{{$table}}' class='button alert'>cancel</a>
{{ Form::close() }}

@stop